package main

import "fmt"

// 结构体方法
type Person struct {
	Name   string
	Age    int
	Sex    string
	Height int
}

func (p Person) PrintInfo() {
	fmt.Printf("姓名：%v  年龄：%v", p.Name, p.Age)
}

func (p *Person) SetInfo(name string, age int) {
	p.Name = name
	p.Age = age
}

func main() {
	//用make创建map类型的数据
	/*var userinfo = make(map[string]string)
	userinfo["username"] = "小青"
	userinfo["age"] = "20"
	userinfo["sex"] = "女"
	fmt.Println(userinfo["username"])
	fmt.Println(userinfo["age"])
	fmt.Println(userinfo["sex"])*/

	//map类型支持在声明时初始化
	/*var userinfo = map[string]string{
		"username": "小白",
		"age":      "40",
		"sex":      "女",
	}
	fmt.Println(userinfo["username"])
	fmt.Println(userinfo["age"])
	fmt.Println(userinfo["sex"])*/

	//for range循环遍历map类型的数据
	/*var userinfo = map[string]string{
		"username": "张文",
		"age":      "30",
		"sex":      "男",
	}
	for k, v := range userinfo {
		fmt.Printf("key:%v value:%v\n", k, v)
	}*/

	//查找map类型的数据(成功)
	/*v, ok := userinfo["username"]
	fmt.Println(v, ok)*/

	//查找失败
	/*v, ok := userinfo["pass"]
	fmt.Println(v, ok)*/

	//map数据的删除操作
	/*var userinfo = map[string]string{
		"username": "忘川",
		"age":      "30",
		"sex":      "女",
		"height":   "164",
	}
	fmt.Println(userinfo)

	delete(userinfo, "sex")
	fmt.Println(userinfo)

	delete(userinfo, "age")
	fmt.Println(userinfo)

	userinfo["username"] = "王亮"
	fmt.Println(userinfo)*/

	/*
		//想在切片里放一系列用户的信息，这时候应该定义一个元素为map类型的切片
		var userinfo = make([]map[string]string, 2, 2)
		//map没有初始化的时候默认为nil
		if userinfo[0] == nil {
			userinfo[0] = make(map[string]string)
			userinfo[0]["username"] = "晓丽"
			userinfo[0]["age"] = "39"
			userinfo[0]["sex"] = "女"
		}
		if userinfo[1] == nil {
			userinfo[1] = make(map[string]string)
			userinfo[1]["username"] = "无恙"
			userinfo[1]["age"] = "28"
			userinfo[1]["height"] = "164cm"

		}
		//fmt.Println(userinfo)
		for _, v := range userinfo {
			//fmt.Println(v)
			for key, value := range v {
				fmt.Printf("key:%v--value:%v\n", key, value)
			}
		}*/

	//值为切片类型的map
	/*var userinfo = make(map[string][]string)
	userinfo["hobby"] = []string{
		"音乐",
		"看书",
		"逛街",
	}
	userinfo["work"] = []string{
		"php",
		"golang",
		"docker",
	}
	for _, v := range userinfo {
		for _, value := range v {
			fmt.Println(value)
		}
	}*/
	/*for k, v := range userinfo {
		fmt.Println(k, v)
	}*/

	//func(接收者变量  接收者类型) 方法名（参数列表）（返回参数）{}
	var p1 = Person{
		Name: "张三",
		Age:  20,
		Sex:  "男",
	}
	p1.PrintInfo()
	p1.SetInfo("王明", 32)
	p1.PrintInfo()
}
